package com.xique.common.datascope.aspect;

import com.xique.common.core.constant.Constants;
import com.xique.common.core.web.domain.AjaxResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;

/**
 * 获取字典标签值的切面
 * @author xl.liu
 * @date 2019-12-20
 */
@Aspect
@Component
public class SetDictLabelAspect {

	/**自定service*/
	@Autowired
	private SetDictLableUtil beanUtil;

	@Around("@annotation(com.xique.common.datascope.annotation.NeedSetDictLabel)")
	public Object setDictLabel(ProceedingJoinPoint point) throws Throwable{
		Object res = point.proceed();
        if (res instanceof Collection) {
        	Collection col = (Collection) res;
        	if(!col.isEmpty()) {
        		this.beanUtil.setValue(col);
        	}
        } else if (res instanceof AjaxResult) {
        	Collection col = (Collection) ((Map) ((AjaxResult) res).get(AjaxResult.DATA_TAG)).get("list");
			if(!col.isEmpty()) {
				this.beanUtil.setValue(col);
			}
		} else {
        	//add by xl.liu 后面需要修改
            this.beanUtil.setValue(res);
        }
        return res;
	}
}
